/**
 * 
 */
package com.sohu.peak.gallery.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.sohu.peak.gallery.dao.TagGroupDao;
import com.sohu.peak.gallery.domain.Tag;
import com.sohu.peak.gallery.domain.TagGroup;
import com.sohu.peak.util.HibernateHelper;
import com.sohu.peak.util.Pagination;

/**
 * @author yuechong
 *
 */
public class TagGroupHibernateDao extends BaseHibernateDAO implements TagGroupDao {

	/* (non-Javadoc)
	 * @see com.sohu.peak.gallery.dao.TagGroupDao#del(com.sohu.peak.gallery.domain.TagGroup)
	 */
	public void del(TagGroup tagGroup) {
		try {
			getSession().delete(tagGroup);
		} catch (RuntimeException re) {
			re.printStackTrace();
		}	
	}

	/* (non-Javadoc)
	 * @see com.sohu.peak.gallery.dao.TagGroupDao#getTagList(com.sohu.peak.util.Pagination)
	 */
	@SuppressWarnings("unchecked")
	public List<TagGroup> getTagGroupList(Pagination pagination) {
		Session session = getSession();

		StringBuffer queryStr = new StringBuffer();
		ArrayList<Integer> paramsList=new ArrayList<Integer>();
		
		queryStr.append("from TagGroup "); 
		queryStr.append("where 1=1 ");

		queryStr.append("order by tagGroupPrority desc,id desc ");
		List<TagGroup> list = (List<TagGroup>) HibernateHelper.pageQuery(session, session.createQuery(queryStr.toString()), pagination, paramsList.toArray());
		
		return list;
	}

	/* (non-Javadoc)
	 * @see com.sohu.peak.gallery.dao.TagGroupDao#getTagList(java.lang.String, com.sohu.peak.util.Pagination, java.lang.Object[])
	 */
	@SuppressWarnings("unchecked")
	public List<TagGroup> getTagGroupList(String queryString, Pagination pagination,Object[] params) {
		List<TagGroup> tagList = null;
		try {
			getSession().flush();
			getSession().clear();
			tagList = (List<TagGroup>) HibernateHelper.pageQuery(getSession(),getSession().createQuery(queryString), pagination, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tagList;
	}

	/* (non-Javadoc)
	 * @see com.sohu.peak.gallery.dao.TagGroupDao#merge(com.sohu.peak.gallery.domain.TagGroup)
	 */
	public TagGroup merge(TagGroup tagGroup) {
		try {
			TagGroup result = (TagGroup) getSession().merge(tagGroup);
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TagGroup getTagGroup(Long tagGroupId) {
		try {
			TagGroup instance = (TagGroup) getSession().get(TagGroup.class, tagGroupId);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public TagGroup getTagGroupByName(String tagGroupName) {
		try {
			Session session = getSession();
			String queryString = "from TagGroup as model where model.tagGroupName = ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, tagGroupName);
			List<TagGroup> result=queryObject.list();
			if(result!=null&&result.size()>0)
				return (TagGroup)result.get(0);
			return null;
		} catch (RuntimeException re) {
			throw re;
		}
	}

}
